package com.wires.lu.practice;


import java.lang.ref.SoftReference;

/**
 * 软引用
 */
public class SoftReferenceDemo {
    /**
     * 内存够用即保留 ，不足够就回收
      */
    public static  void softRef_Memory_Enough(){
        Object obj=new Object();
        SoftReference<Object> softReference=new SoftReference<>(obj);
        System.out.println(obj);
        System.out.println(softReference.get());
        obj=null;
        System.gc();
        System.out.println(obj);
        System.out.println(softReference.get());
    }

    public static  void  softRef_Memory_NotEnough(){
        Object obj=new Object();
        SoftReference<Object> softReference=new SoftReference<>(obj);
        System.out.println(obj);
        System.out.println(softReference.get());
        obj=null;
        try {
            byte [] bytes=new byte[30*1024*1024];
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println(obj);
            System.out.println(softReference.get());
        }
    }

    public static void main(String[] args) {
        softRef_Memory_Enough();
        //softRef_Memory_NotEnough();
    }



}
